package gwtI18nEditor.datamodel;

import gwtI18nEditor.resources.Country;
import gwtI18nEditor.resources.Language;

public class Locale {

	private final Language language;
	private final Country  country;
	private final String   description;
	private final String   code;
	
	public Locale(String languageCode, String countryCode) {
		code     = languageCode+((countryCode==null) ? "" : "_"+countryCode);
		language = Language.get(languageCode);
		country  = (language==null) ? null : language.getCountry(countryCode);
		if (language==null) {
			if ((countryCode==null) || (countryCode.isEmpty())) {
				description = languageCode;
			} else {
				description = languageCode+" ("+countryCode+")";
			}
		} else {
			if ((countryCode==null) || (countryCode.isEmpty())) {
				description = language.getName();
			} else {
				description = language.getName()+" ("+((country==null) ? countryCode : country.getName())+")"; 
			}
		}
	}

	public Language getLanguage() {
		return language;
	}

	public Country getCountry() {
		return country;
	}
	
	public String getDescription() {
		return description;
	}

	public String getCode() {
		return code;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((country == null) ? 0 : country.hashCode());
		result = prime * result + ((description == null) ? 0 : description.hashCode());
		result = prime * result + ((language == null) ? 0 : language.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof Locale))
			return false;
		Locale other = (Locale) obj;
		if (country != other.country)
			return false;
		if (description == null) {
			if (other.description != null)
				return false;
		} else if (!description.equals(other.description))
			return false;
		if (language != other.language)
			return false;
		return true;
	}
}
